<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Prunus Tabs
 *
 * Contenedor de Tabs de ui.jquery
 *
 * @author		agosto
 * @date		  20090728
 * @version		0.1
 * @copyright	Copyright (c) 2009, 7dedos
 * @link		http://www.7dedos.com.ar
 */
/* Estructura del array DATA ******************************************************************************
  $id - No es un elemento del array, es la clave de cada grupo de tabs
    $tabs - array con las tabs del id en particular
      el id de cada tab es el id en el array
      $title - titulo del tab
      $content - contenido html del tab
    $selectedIndex - index (no id, sino el indice con base cero del tab) del tab seleccionado por default - si no se setea, se selecciona el primer tab
*/
  
include_once ('prunus_buffer.php');

class prunus_tabs
{
	protected $ci;
  protected $buffer;
  private $data; //array principal
    
	public function __construct()	{
		$this->ci =& get_instance();
    $this->ci->load->helper('form');
    $this->ci->load->config('prunus_tabs');
    $this->buffer = new prunus_buffer($this->ci->config->item('PRTA_defaultOffset'));
    
		log_message('debug', 'prunus_tabs inicializado');
	}
  
  public function addTab($tabGroupId='', $tabId='', $title='', $content) {
    if ($tabGroupId == '') $tabGroupId = 'tgrp1';
    if ($tabId == '')      $tabId = "tab" . count($this->data[$tabGroupId]['tabs']);
    if ($title == '')      $title = $this->ci->prunus_core->getMenuItemName();
    $this->data[$tabGroupId]['tabs'][$tabId]['title'] = $title;
    $this->data[$tabGroupId]['tabs'][$tabId]['content'] = $content;
  }
  
  public function setSelected($tabGroupId, $tabId) {
    if ($tabGroupId == '') { $tabGroupId = 'tgrp1'; }
    $this->data[$tabGroupId]['selectedIndex'] = $tabId;
  }
  
  public function render() {
    $this->buffer->clear();
    $this->buffer->addLine(2, "<script type=\"text/javascript\">");
    $this->buffer->addLine(4, "//<![CDATA[");
    $this->buffer->addLine(4, "$(function() {");
    $selected = "";
    foreach ($this->data as $groupId => $groupData) { 
      if (isset($groupData['selectedIndex'])) $selected = "{ selected: " . $groupData['selectedIndex'] . " }"; 
      $this->buffer->addLine(4, "$(\"#" . $groupId . "\").tabs( $selected );"); 
    }
    $this->buffer->addLine(4, "});");
    $this->buffer->addLine(4, "//]]>");
    $this->buffer->addLine(2, "</script>");
  
    foreach ($this->data as $groupId => $groupData) { 
      $this->buffer->addLine(2, "<div id=\"" . $groupId . "\">"); 
      $this->buffer->addLine(4, "<ul>"); 
      foreach ($groupData['tabs'] as $tabId => $tabData) {
        $this->buffer->addLine(6, "<li><a href=\"#" . $tabId . "\">" . $tabData['title'] . "</a></li>"); 
      }
      $this->buffer->addLine(4, "</ul>"); 
    }
    foreach ($groupData['tabs'] as $tabId => $tabData) {
      $this->buffer->addLine(4, "<div id=\"" . $tabId . "\">"); 
      $this->buffer->addLine(6, $tabData['content']); 
      $this->buffer->addLine(4, "</div>"); 
    }
    $this->buffer->addLine(2, "</div>"); 
    return $this->buffer->get();
  } //render
  
}
